/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.wordplat.quickstart.utils;

import com.wordplat.quickstart.ResourceTable;

import ohos.agp.components.*;
import ohos.agp.utils.LayoutAlignment;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

/**
 * ToastUtil
 *
 * @since 2021-03-29
 */
public class ToastUtil {
    private static final int NUM_5000 = 5000;
    private static final int RADIOUS = 58;
    private static final int NUM_50 = 50;
    Context context;

    /**
     * ToastUtil
     *
     * @param context
     */
    public ToastUtil(Context context) {
        this.context = context;
    }

    /**
     * toastCtx
     *
     * @param ctx
     * @param text
     */
    public static void toastCtx(Context ctx, String text) {
        new ToastDialog(ctx)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAutoClosable(true)
                .setDuration(NUM_5000)
                .setCornerRadius(RADIOUS)
                .show();
    }

    /**
     * toastCtx
     *
     * @param ctx
     * @param layout
     */
    public static void toastCtx(Context ctx, Component layout) {
        Component customToastLayout = LayoutScatter.getInstance(ctx).parse(layout.getId(), null, false);
        ToastDialog toastDialog = new ToastDialog(ctx);
        toastDialog.setComponent(customToastLayout);
        toastDialog.setCornerRadius(RADIOUS);
        toastDialog.setSize(DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.CENTER);
        toastDialog.show();
    }

    /**
     * toast
     *
     * @param text
     */
    public void toast(String text) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAutoClosable(true)
                .setDuration(NUM_5000)
                .setCornerRadius(RADIOUS)
                .show();
    }

    /**
     * toast
     *
     * @param text
     * @param duration
     */
    public void toast(String text, int duration) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setDuration(duration)
                .setCornerRadius(RADIOUS)
                .show();
    }

    /**
     * toast
     *
     * @param text
     * @param duration
     * @param offsetX
     * @param offsetY
     * @param gravity
     */
    public void toast(String text, int duration, int offsetX, int offsetY, int gravity) {
        new ToastDialog(context)
                .setText(text)
                .setSize(ComponentContainer.LayoutConfig.MATCH_PARENT, ComponentContainer.LayoutConfig.MATCH_CONTENT)
                .setAlignment(gravity)
                .setDuration(duration)
                .setCornerRadius(RADIOUS)
                .setOffset(offsetX, offsetY)
                .show();
    }

    /**
     * toast
     *
     * @param context
     * @param text
     */
    public static void toast(Context context, String text) {
        ComponentContainer customToastLayout = (ComponentContainer) LayoutScatter.getInstance(context)
                .parse(ResourceTable.Layout_toast_dialog_layout, null, false);

        Text textView = (Text) customToastLayout.findComponentById(ResourceTable.Id_toast_text);

        textView.setText(text);

        DirectionalLayout.LayoutConfig layoutConfig = new DirectionalLayout.LayoutConfig(
                DirectionalLayout.LayoutConfig.MATCH_CONTENT, DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        layoutConfig.setMarginBottom(NUM_50);
        customToastLayout.setLayoutConfig(layoutConfig);

        ToastDialog toastDialog = new ToastDialog(context);
        toastDialog.setComponent(customToastLayout);
        toastDialog.setCornerRadius(RADIOUS);
        toastDialog.setDuration(NUM_5000);
        toastDialog.setSize(ComponentContainer.LayoutConfig.MATCH_CONTENT,
                DirectionalLayout.LayoutConfig.MATCH_CONTENT);
        toastDialog.setAlignment(LayoutAlignment.BOTTOM | LayoutAlignment.HORIZONTAL_CENTER);
        toastDialog.show();
    }
}
